package org.timemachine.factory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.timemachine.factory.spi.EntityChange;
import org.timemachine.tool.ClassHelperImpl;

public class EntityChangeFactory {

	public static String ENTITYCHANGE_PACKAGENAME = "org.timemachine.model.app.";

	public static String NAME_CHANGE_TYPE = "changeType";
	public static String NAME_BEGIN_CHANGE_INFO = "beginChangeInfo";
	public static String NAME_END_CHANGE_INFO = "endChangeInfo";
	public static String NAME_CHANGE_ID = "changeId";
	
	public static int CHANGE_TYPE_INSERT = 1;
	public static int CHANGE_TYPE_UPDATE = 2;
	public static int CHANGE_TYPE_REMOVE = 3;
	public static String CHANGE_NAME_INSERT = "EKLEME";
	public static String CHANGE_NAME_UPDATE = "GÜNCELLEME";
	public static String CHANGE_NAME_REMOVE = "SİLME";

	public static EntityChange convertEntityChange(Object entity) {

		EntityChange entityChange = null;
		Class<?> cls;
		try {
			cls = Class.forName(entity.getClass().getName() + "Change");

			Object objEntityChange = cls.newInstance();

			entityChange = (EntityChange) objEntityChange;
			// entityChange = copyProperties(objEntityChange, entity);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return entityChange;
	}

	public static Object convertChange(Object entity) {

		Object change = null;
		Class<?> cls;
		try {
			cls = Class.forName(entity.getClass().getName() + "Change");

			change = cls.newInstance();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return change;
	}


	public static EntityChange copyProperties(Object objEntityChange,
			Object entity) throws IllegalAccessException,
			InvocationTargetException, Exception {

		//BeanUtils.copyProperties(objEntityChange, entity);
		ClassHelperImpl clsHlp = new ClassHelperImpl();
		Field[] fields = clsHlp.findAllDeclaredFieldsWithSuperClasses(entity.getClass());
		for (Field field : fields) {
			try {
				if ((!field.getType().equals(Date.class) && !field.getType().equals(BigDecimal.class))
						|| clsHlp.findFieldValue(entity, entity.getClass(), field.getName())!=null) {				
					BeanUtils.copyProperty(objEntityChange, field.getName(), clsHlp.findFieldValue(entity, entity.getClass(), field.getName()));			
				}				
			} catch (NoSuchFieldException e) {
				// TODO: handle exception
			}
		}

		EntityChange entityChange = (EntityChange) objEntityChange;
		return entityChange;
	}

	public static Object copyPropertiesEntity(Object entity,
			Object objEntityChange) throws IllegalAccessException,
			InvocationTargetException {

		BeanUtils.copyProperties(entity, objEntityChange);

		return entity;
	}

	public static EntityChange copyProperty(Object objEntityChange,
			String propertyName, Object propertyValue)
			throws IllegalAccessException, InvocationTargetException {

		BeanUtils.copyProperty(objEntityChange, propertyName, propertyValue);

		EntityChange entityChange = (EntityChange) objEntityChange;
		return entityChange;
	}

}
